import React from 'react';
import { BrowserRouter as Router, Route } from "react-router-dom";
import Header from 'shared/components/Header';
import localStorage from 'shared/utils/localStorage';
import CheckPayStatus from 'shared/components/CheckPayStatus'
import Home from './Home';
import Product from './Product'
import AllProduct from './AllProduct';
import Cart from './Cart'
import OrderDetails from './OrderDetails';
import InvoiceForm from './Product/InvoiceForm';
import PaymentSuccess from './OrderDetails/PaymentSuccess';
import Account from './Account';
import AddressForm from './Account/AddressManage/AddressForm';
import EditPhoneForm from './Account/EditPhoneForm'
import FriendLink from './Home/FriendLink/detail';
import { Form, Login } from './LoginModule'
import ActivityDetail from './Home/activity/activityDetail';
import SecrecyProtocols from './Home/FriendLink/secrecyProtocols';
import fetchSessIdApi from './api';
import './index.less';

const routes = [
  { path: '/activityDetail/:id', exact: true, component: ActivityDetail },
  { path: '/', exact: true, component: Home },
  { path: '/category', exact: true, component: Home },
  { path: '/productDetail/:id', exact: false, component: Product },
  { path: '/allProduct', exact: false, component: AllProduct },
  { path: '/cart', exact: true, component: Cart },
  { path: '/account', exact: false, component: Account },
  { path: '/OrderDetails/:type', exact: false, component: OrderDetails },
  { path: '/InvoiceForm', exact: false, component: InvoiceForm },
  { path: '/PaymentSuccess', exact: true, component: PaymentSuccess},
  { path: '/addressForm', exact: true, component: AddressForm },
  { path: '/addressForm/:addressId', exact: true, component: AddressForm },
  { path: '/editPhoneForm', exact: true, component: EditPhoneForm },
  { path: '/forgetPassword', exact: true, component: () => <Form isRegisterForm={false} /> },
  { path: '/editPassword', exact: true, component: () => <Form isEditPasswordForm /> },
  { path: '/register', exact: true, component: () => <Form isRegisterForm /> },
  { path: '/Login', exact: true, component: Login },
  { path: '/friendLink/:id', exact: true, component: FriendLink },
  { path: '/checkPayStatus', exact: true, component: CheckPayStatus },
  { path: '/secrecyProtocols', exact: true, component: SecrecyProtocols }
]

export default class app extends React.Component {

  componentDidMount() {
    // var userAgentInfo = navigator.userAgent.toLowerCase();
    // var Agents = ["android", "iphone",
    //   "symbianos", "windows phone",
    //   "ipad", "ipod"];
    // for (var v = 0; v < Agents.length; v++) {
    if (!isMobile) {
      const { pathname } = __self.history.location
      if (pathname === '/') {
        location.href = window.url;
      }
    }

    fetchSessIdApi().then(({ data: { sess_id } }) => {
      localStorage.set('sessId', sess_id)
    })
  }

  render() {
    return (
      <div>
        <Header />
        <Router basename={window.selfBasename || "/ecm"}>
          <div className="mian-content">
            {
              routes.map((item, index) => (
                <Route
                  key={index}
                  path={item.path}
                  exact={item.exact}
                  component={(props) => {
                    __self.history = props.history;
                    return (<item.component {...props} />)
                  }}
                />)
              )
            }
          </div>
        </Router>
      </div>
    );
  }
}